package com.zixun.controller;

import com.google.gson.Gson;
import com.zixun.config.WebApiConfig;
import com.zixun.model.ApplyInfo;
import com.zixun.model.FeedBack;
import com.zixun.model.Industry;
import com.zixun.model.User;
import com.zixun.repository.ApplyInfoRepository;
import com.zixun.repository.FeedBackRespository;
import com.zixun.repository.IndustryRepository;
import com.zixun.repository.UserRepository;
import com.zixun.service.UserService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Transactional
@RequestMapping("api/user/v2")
public class UserV2WebApi {

    @Autowired
    ApplyInfoRepository applyInfoRepository;

    @Autowired
    IndustryRepository industryRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    FeedBackRespository feedBackRespository;

    @Autowired
    UserService userService;

    /**
     *
     * @api {GET} /api/user/v2/getIndustry 获取行业列表
     * @apiName getIndustry
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 获取行业列表
     * @apiParam {Integer} id 行业编号，一级传0，之后传对应ID
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,8:无数据, 整型】,
     * "errMsg":【错误信息, 字符串】.
     * "data":[{
     *      "id":【编号】,
     *      "name":【名称】
     * }]
     */
    @RequestMapping("getIndustry")
    public Map getIndustry(Integer id){
        Map result = new HashMap();
        System.out.println(id+" +++++++++++++");
        List<Industry> list  = industryRepository.findAllByParentId(id);
        result.put("returnValue",1);
        result.put("errMsg",null);
        result.put("data",list);
        return result;
    }


    /**
     * @api {POST} /api/user/v2/applyExpertByNoWorkperson 申请行家——无业人员
     * @apiName applyExpertByWorkperson
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 申请行家——无业人员
     * @apiParamExample 请求参数{JSON}
     * {
     * "userId":【用户编号, 字符串】,
     * "realName":【姓名,字符串】,
     * "takeIdNumber":【手持身份证,字符串】,
     * "sex":【性别，整形】,
     * "mobilephone":【手机号码，字符串】,
     * "edu":【学历，整形】,
     * "address":【所在区域，字符串】,
     * "service":【服务范围，字符串】,
     * "expertise":【专长，字符串】,
     * "tjPerson":【推荐人，字符串】
      * }
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功,2:参数错误,3:服务器异常, 整型】,
     * "errMsg":【错误信息 , 字符串】,
     * }
     */
    @RequestMapping("applyExpertByNoWorkperson")
    public Object applyExpertByNoWorkperson(@RequestBody String data){
        Map result = new HashMap();
        ApplyInfo applyInfo = new Gson().fromJson(data, ApplyInfo.class);
        applyInfo.setType(0);
        applyInfo.setStatus(0);
        applyInfoRepository.save(applyInfo);
        result.put("returnValue",1);
        result.put("errMsg",null);
        return result;
    }

    /**
     * @api {POST} /api/user/v2/applyExpertByWorkperson 申请行家——在职人员
     * @apiName applyExpertByWorkperson
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 申请行家——在职人员
     * @apiParamExample 请求参数{JSON}
     * {
     * "userId":【用户编号, 字符串】,
     * "realName":【姓名,字符串】,
     * "takeIdNumber":【手持身份证,字符串】,
     * "sex":【性别，整形】,
     * "mobilephone":【手机号码，字符串】,
     * "edu":【学历，整形】,
     * "address":【所在区域，字符串】,
     * "company":【单位，字符串】,
     * "department":【部门，字符串】,
     * "post":【岗位，字符串】,
     * "companyTitle":【职称，字符串】,
     * "service":【服务范围，字符串】,
     * "expertise":【专长，字符串】,
     * "jobCard":【工作证,字符串】,
     * "tjPerson":【推荐人，字符串】,
     * "industryId":【行业编号,整形】
     * }
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功,2:参数错误,3:服务器异常, 整型】,
     * "errMsg":【错误信息 , 字符串】,
     * }
     */
    @RequestMapping("applyExpertByWorkperson")
    public Object applyExpertByWorkperson(@RequestBody String data){
        Map result = new HashMap();
        ApplyInfo applyInfo = new Gson().fromJson(data, ApplyInfo.class);
        applyInfo.setType(0);
        applyInfo.setStatus(0);
        applyInfoRepository.save(applyInfo);
        result.put("returnValue",1);
        result.put("errMsg",null);
        return result;
    }

    /**
     * @api {POST} /api/user/v2/feedback 意见反馈
     * @apiName feedback
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 意见反馈
     * @apiParamExample 请求参数{JSON}
     * {
     * "userId":【用户编号, 字符串】,
     * "content":【内容,字符串】,
     * }
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功,2:参数错误,3:服务器异常,11:JSON参数错误 ,整型】,
     * "errMsg":【错误信息 , 字符串】,
     * }
     */
    @RequestMapping("feedback")
    public Map<String ,Object> feedback(@RequestBody String data) {
        Map<String, Object> result = new HashMap();
        FeedBack feedBack = new FeedBack();
        try {
            JSONObject jo = new JSONObject(data);
            String userId = jo.getString("userId");
            String content = jo.getString("content");
            if (userId == null || content == null) {
                result.put("errMsg", "参数错误");
                result.put("returnValue", 2);
                return result;
            }
            feedBack.setCreateTime(new Date(System.currentTimeMillis()));
            feedBack.setUserId(Integer.valueOf(userId));
            feedBack.setContent(content);
            feedBackRespository.save(feedBack);
            result.put("returnValue", 1);
            result.put("errMsg", "成功");
            return result;
        }catch (JSONException e){
            e.printStackTrace();
            result.put("returnValue",11);
            result.put("errMsg","JSON参数异常");
            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg", "服务器异常");
            result.put("returnValue", 3);
            return  result;
        }
    }

    /**
     *
     * @api {GET} /api/user/v2/getBalance 获取账户余额
     * @apiName getBalance
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 获取账户余额
     * @apiParam {String} userId 用户编号
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,10:用户不存在, 整型】,
     * "errMsg":【错误信息, 字符串】
     * "data":{
     *      "balance":【余额, 整型】,
     *      "alipayAccount":【支付宝帐号, 字符串】
     * }
     * }
     */
    @RequestMapping("getBalance")
    public Object getBalance(String userId){
        Map result = new HashMap();
        Integer balance;
        Map data = new HashMap();
        try {
            if(userId==null){
                result.put("errMsg","参数错误");
                result.put("returnValue",2);
                return result;
            }
            User user=userRepository.findById(Integer.valueOf(userId));
            if(user ==null){
                result.put("errMsg",10);
                result.put("returnValue","用户不存在");
                return result;
            }
            if(user.getBalance()==null){
                balance=0;
                user.setBalance(0);
                userRepository.save(user);
            }else {
                balance=user.getBalance();
            }
            data.put("balance",balance);
            data.put("alipayAccount",user.getAlipayAccount());
            result.put("errMsg","成功");
            result.put("returnValue",1);
            result.put("data",data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("returnValue","服务器异常");
            result.put("errMsg",3);
            return result;
        }
    }
    /**
     *
     * @api {GET} /api/user/v2/myZone 我的地盘
     * @apiName myZone
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 我的地盘
     * @apiParam {String} userId 用户编号
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,10:用户不存在, 整型】,
     * "errMsg":【错误信息, 字符串】
     * "data":{
     *      "balance":【余额, 整型】,
     *      "nickName":【昵称, 字符串】,
     *      "headPicture":【头像, 字符串】,
     *      "level":【等级, 整型】,
     *      "userId":【用户编号, 字符串】,
     *      "starLevel":【星级, 整型】,
     *      "integral":【积分, 整型】,
     * }
     * }
     */
    @RequestMapping("myZone")
    public Map<String,Object> myZone(String userId){
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        try {
            if(userId==null){
                result.put("errMsg","参数错误");
                result.put("returnValue",2);
                return result;
            }
            if(!userService.isUserExist(Integer.valueOf(userId))){
                result.put("errMsg","用户不存在");
                result.put("returnValue",10);
                return result;
            }
            User user=userService.getUserByUserId(Integer.valueOf(userId));
            data.put("nickName",user.getNickName());
            data.put("headPicture", WebApiConfig.IMAGE_URL+user.getHeadPicture());
            if(user.getLevel()==null){
                user.setLevel(0);
                userRepository.save(user);
            }
            data.put("level",user.getLevel());
            data.put("userId",user.getId().toString());
            if(user.getStarLevel()==null){
                user.setStarLevel(0);
                userRepository.save(user);
            }
            data.put("startLevel",user.getStarLevel());
            if(user.getBalance()==null){
                user.setBalance(0);
                userRepository.save(user);
            }
            data.put("balance",user.getBalance());
            if(user.getIntegral()==null){
                user.setIntegral(0);
                userRepository.save(user);
            }
            data.put("integral",user.getIntegral());
            result.put("errMsg","成功");
            result.put("returnValue",1);
            result.put("data",data);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg","服务器异常");
            result.put("returnValue",3);
            return result;
        }
    }

    /**
     *
     * @api {GET} /api/user/v2/getMyIntegral 我的积分
     * @apiName getMyIntegral
     * @apiGroup user
     * @apiVersion 0.0.1
     * @apiDescription 我的积分
     * @apiParam {String} userId 用户编号
     * @apiSuccessExample 返回值: HTTP/1.1 200 OK
     * {
     * "returnValue":【1:成功, 2:参数错误,3:服务器异常,10:用户不存在, 整型】,
     * "errMsg":【错误信息, 字符串】,
     * "data":【积分, 整型】
     * }
     */
    @RequestMapping("getMyIntegral")
    public Map<String,Object> getMyIntegral(String userId){
        Map<String,Object> result =new HashMap<>();
        try {
            if(userId==null){
                result.put("returnValue",2);
                result.put("errMsg","参数错误");
                return result;
            }
            if(!userService.isUserExist(Integer.valueOf(userId))){
                result.put("errMsg","用户不存在");
                result.put("returnValue",10);
                return result;
            }
            User user = userService.getUserByUserId(Integer.valueOf(userId));
            if(user.getIntegral()==null){
                user.setIntegral(0);
                userRepository.save(user);
            }
            result.put("data",user.getIntegral());
            result.put("errMsg","成功");
            result.put("returnValue",1);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("errMsg","服务器异常");
            result.put("returnValue",3);
            return result;
        }
    }

}
